/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.SysAdmin;

import csse3004.GD.DA.SysAdminDA;
import java.util.StringTokenizer;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


/**
 *
 * @author s4201567 Tony Yu
 */
public class ManagePADetailsController implements Controller {
    //private SysAdminService sds = new SysAdminDA();
    protected final Log logger = LogFactory.getLog(getClass());
    private SysAdminService sds = new SysAdminDA();
    
    @RequestMapping(method = RequestMethod.GET)    
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

      try {
            if (hsr.getParameter("pid") != null && hsr.getParameter("type") != null) {
                // password
                int pid = Integer.parseInt(hsr.getParameter("pid"));

                // DB function to send new password to poll admin
                String pwd = "A new password has been successfully sent!";
                logger.info("Text: " + pwd);
                return new ModelAndView("successMsg", "pwd", pwd);
            }

            if (hsr.getParameter("pid") != null) {
                int pid = Integer.parseInt(hsr.getParameter("pid"));
                String email = sds.getUserEmail(pid);
                String expiry = sds.getUserExpiry(pid);
                //String expiryTok;

//                StringTokenizer tok = new StringTokenizer(expiry);
//                expiryTok = (String)tok.nextElement();

                ModelAndView mnv = new ModelAndView("managePADetails");
                mnv.addObject("pid", pid);
                mnv.addObject("email", email);
                mnv.addObject("expiry", expiry);
                return mnv;
            }
            return new ModelAndView("managePADetails");
      } catch (Exception e) {
          logger.info(e);
          return new ModelAndView("managePADetails");
      }
    }
}

/*
            <form:form method="post" commandName="managePADetails">
              <table bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5">
                <tr>
                  <td>Expires in:</td>
                    <td>
                        <form:input path="numExpiryDays"/>
                    </td>
                    <td>
                        <form:errors path="numExpiryDays" cssClass="errors"/>
                    </td>
                </tr>
                <tr>
                  <td>Password:</td>
                    <td>
                      <form:input path="password" value="123" type="password"/> 
                    </td><td>Remove value attribute in password!</td>
                    <td>
                        <form:errors path="password" cssClass="errors"/>
                    </td>
                </tr>
                <tr>
                  <td>Email:</td>
                    <td>
                      <form:input path="email"/>
                    </td>
                </tr>
              </table>
              <br>
              <input type="submit" align="center" value="Submit">
              <input type="reset" value="Reset" name="reset"><br/><br/>
              <form>
                  <input type="button" onClick="window.location.href='managePA.html'" value="Back"/>
              </form>
            </form:form>
*/
